package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;

import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.StockCheckPlanDetailMapper;
import com.canaan.business.domain.storehouse.StockCheckPlanDetail;
import com.canaan.business.service.storehouse.IStockCheckPlanDetailService;

/**
 * 库存盘点计划明细Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-14
 */
@Service
public class StockCheckPlanDetailServiceImpl implements IStockCheckPlanDetailService {
    @Resource
    private StockCheckPlanDetailMapper stockCheckPlanDetailMapper;

    /**
     * 查询库存盘点计划明细
     *
     * @param id 库存盘点计划明细主键
     * @return 库存盘点计划明细
     */
    @Override
    public StockCheckPlanDetail selectStockCheckPlanDetailById(Long id) {
        return stockCheckPlanDetailMapper.selectStockCheckPlanDetailById(id);
    }

    /**
     * 查询库存盘点计划明细列表
     *
     * @param stockCheckPlanDetail 库存盘点计划明细
     * @return 库存盘点计划明细
     */
    @Override
    public List<StockCheckPlanDetail> selectStockCheckPlanDetailList(StockCheckPlanDetail stockCheckPlanDetail) {
        return stockCheckPlanDetailMapper.selectStockCheckPlanDetailList(stockCheckPlanDetail);
    }

    /**
     * 新增库存盘点计划明细
     *
     * @param stockCheckPlanDetail 库存盘点计划明细
     * @return 结果
     */
    @Override
    public int insertStockCheckPlanDetail(StockCheckPlanDetail stockCheckPlanDetail) {
        if(StringUtils.isBlank(stockCheckPlanDetail.getNo())){
            stockCheckPlanDetail.setNo(TableNoUtil.getNo(StockCheckPlanDetail.class));
        }
        stockCheckPlanDetail.setCreateBy(SecurityUtils.getUsername());
        stockCheckPlanDetail.setCreateTime(DateUtils.getNowDate());
        return stockCheckPlanDetailMapper.insertStockCheckPlanDetail(stockCheckPlanDetail);
    }

    /**
     * 批量新增库存盘点计划明细
     *
     * @param stockCheckPlanDetailList 库存盘点计划明细列表
     * @return 结果
     */
    @Override
    public int batchInsertStockCheckPlanDetail(List<StockCheckPlanDetail> stockCheckPlanDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockCheckPlanDetail stockCheckPlanDetail : stockCheckPlanDetailList){
            if(StringUtils.isBlank(stockCheckPlanDetail.getNo())){
                stockCheckPlanDetail.setNo(TableNoUtil.getNo(StockCheckPlanDetail.class));
            }
            stockCheckPlanDetail.setCreateBy(username);
            stockCheckPlanDetail.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<stockCheckPlanDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckPlanDetailList.size()) {
                endIndex = stockCheckPlanDetailList.size();
            }
            insertNum = insertNum + stockCheckPlanDetailMapper.batchInsertStockCheckPlanDetail(stockCheckPlanDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改库存盘点计划明细
     *
     * @param stockCheckPlanDetail 库存盘点计划明细
     * @return 结果
     */
    @Override
    public int updateStockCheckPlanDetail(StockCheckPlanDetail stockCheckPlanDetail) {
        stockCheckPlanDetail.setUpdateBy(SecurityUtils.getUsername());
        stockCheckPlanDetail.setUpdateTime(DateUtils.getNowDate());
        return stockCheckPlanDetailMapper.updateStockCheckPlanDetail(stockCheckPlanDetail);
    }

    /**
     * 批量修改库存盘点计划明细
     *
     * @param stockCheckPlanDetailList 库存盘点计划明细列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockCheckPlanDetail(List<StockCheckPlanDetail> stockCheckPlanDetailList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockCheckPlanDetail stockCheckPlanDetail : stockCheckPlanDetailList){
            stockCheckPlanDetail.setUpdateBy(username);
            stockCheckPlanDetail.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<stockCheckPlanDetailList.size();) {
            int endIndex = i+500;
            if (endIndex > stockCheckPlanDetailList.size()) {
                endIndex = stockCheckPlanDetailList.size();
            }
            updateNum = updateNum + stockCheckPlanDetailMapper.batchUpdateStockCheckPlanDetail(stockCheckPlanDetailList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除库存盘点计划明细
     *
     * @param ids 需要删除的库存盘点计划明细主键集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckPlanDetailByIds(Long[] ids) {
        return stockCheckPlanDetailMapper.deleteStockCheckPlanDetailByIds(ids);
    }

    /**
     * 删除库存盘点计划明细信息
     *
     * @param id 库存盘点计划明细主键
     * @return 结果
     */
    @Override
    public int deleteStockCheckPlanDetailById(Long id) {
        return stockCheckPlanDetailMapper.deleteStockCheckPlanDetailById(id);
    }

    /**
     * 查询库存盘点计划明细
     *
     * @param no 编号
     * @return 库存盘点计划明细
     */
    @Override
    public StockCheckPlanDetail selectStockCheckPlanDetailByNo(String no) {
        return stockCheckPlanDetailMapper.selectStockCheckPlanDetailByNo(no);
    }

    /**
     * 批量删除库存盘点计划明细
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockCheckPlanDetailByNos(String[] nos) {
        return stockCheckPlanDetailMapper.deleteStockCheckPlanDetailByNos(nos);
    }

    /**
     * 删除库存盘点计划明细信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockCheckPlanDetailByNo(String no) {
        return stockCheckPlanDetailMapper.deleteStockCheckPlanDetailByNo(no);
    }

    @Override
    public List<StockCheckPlanDetail> selectStockCheckPlanDetailByPlanNo(String planNo) {
        StockCheckPlanDetail detail = new StockCheckPlanDetail();
        detail.setPlanNo(planNo);
        return this.selectStockCheckPlanDetailList(detail);
    }
}
